package businesslogic.user;

import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;

import dataService.userDataService.UserHeaders;
import dbHelper.TableNames;
import Enum.Faculty;
import Enum.Gender;
import Enum.Hometown;
import Enum.UserRole;
import po.userpo.Birthday;
import po.userpo.Teacher;
import po.userpo.UserID;
import useDatabase.java.getTable;
import useDatabaseService.GetTableImpli;

public class GetTeacherList {
	final String key = "-";
	GetTableImpli table=new getTable();
	DefaultTableModel model = new DefaultTableModel();
	ArrayList<Teacher> Tchlist = new ArrayList<Teacher>();
	
	public ArrayList<Teacher> list(Faculty f) throws RemoteException{
		String columns = UserHeaders.name()+","+UserHeaders.ID()+","+
				UserHeaders.birth()+","+UserHeaders.age()+","+UserHeaders.faculty()+","+
				UserHeaders.gender()+","+UserHeaders.password()+","+
				UserHeaders.registerTime()+","+UserHeaders.role()+","+UserHeaders.home();
		model = table.getTable(TableNames.userTable(), columns, 
				UserHeaders.roleEqu(UserRole.courseTeacher.toString())+" and "+
				UserHeaders.facultyEqu(f.toString()));
		
		for(int i=0;i<model.getRowCount() ; i++){
			Teacher tch = combine(model.getValueAt(i, 0).toString(),model.getValueAt(i, 1).toString(),
					model.getValueAt(i, 2).toString(),model.getValueAt(i, 3).toString(),model.getValueAt(i, 4).toString(),
					model.getValueAt(i, 5).toString(),model.getValueAt(i, 6).toString(),model.getValueAt(i, 7).toString(),
					model.getValueAt(i, 8).toString(),model.getValueAt(i, 9).toString());
			Tchlist.add(tch);
		}
		
		return Tchlist;
	}
	
	private Teacher combine(String name , String id , String b , String a , 
			String f , String g , String password , String registerTime , String r , String home) throws RemoteException{
		UserID userId = new UserID(id);
		int age = Integer.parseInt(a);
		Faculty faculty = Faculty.valueOf(f);
		Gender gender = Gender.valueOf(g);
		UserRole role = UserRole.valueOf(r);
		Hometown hometown = Hometown.valueOf(home);
		
		String[] split = b.split(key);
		int y,m,d;
		y=Integer.parseInt(split[0]);
		m=Integer.parseInt(split[1]);
		d=Integer.parseInt(split[2]);
		Birthday birth = new Birthday(y,m,d);
		
		Teacher tch = new Teacher(userId, name, null, age,  role,  gender,
				 faculty,  hometown, birth , registerTime);
		
		return tch;
	}
}

